package com.weixing.mall.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.AgentBonusMapper;
import com.weixing.mall.provider.mapper.AgentWithdrawMapper;
import com.weixing.mall.provider.model.domain.AgentBonus;
import com.weixing.mall.provider.model.domain.AgentWithdraw;
import com.weixing.mall.provider.model.query.AgentBonusQuery;
import com.weixing.mall.provider.model.vo.AgentBonusVo;
import com.weixing.mall.provider.model.vo.AgentRebateVo;
import com.weixing.mall.provider.model.vo.AgentWithdrawBonusVo;
import com.weixing.mall.provider.service.IAgentBonusService;
import com.weixing.mall.util.number.NumberPatternEnum;
import com.weixing.mall.util.number.NumberToRmbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 代理商模块-代理商分红表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentBonusServiceImpl extends ServiceImpl<AgentBonusMapper, AgentBonus> implements IAgentBonusService {

    @Autowired
    private AgentWithdrawMapper agentWithdrawMapper;
    @Override
    public IPage<AgentWithdrawBonusVo> agentWithdrawBonusList(Page page, Long id){
        AgentWithdraw agentWithdraw = agentWithdrawMapper.selectById(id);
        return baseMapper.agentWithdrawBonusList(page,id,agentWithdraw.getCreatedTime());
    }

    @Override
    public IPage<AgentBonusVo> selectListOfPage(Page page, AgentBonusQuery agentBonusQuery) {

        IPage<AgentBonusVo> agentBonusVoIPage = baseMapper.selectListOfPage(page, agentBonusQuery);
        List<AgentBonusVo> records = agentBonusVoIPage.getRecords();
        if (CollUtil.isNotEmpty(records)){
            for (AgentBonusVo vo : records){
                vo.setFmtOrderPrice(NumberToRmbUtil.format(vo.getOrderPrice(), NumberPatternEnum.PAT_DEFAULT.getValue()));
                vo.setFmtBonusAmount(NumberToRmbUtil.format(vo.getBonusAmount(), NumberPatternEnum.PAT_DEFAULT.getValue()));
            }
        }
        return agentBonusVoIPage;
    }

    @Override
    public List<AgentBonusVo> selectList(AgentBonusQuery agentBonusQuery) {
        return baseMapper.selectList(agentBonusQuery);
    }

    @Override
    public BigDecimal countBonus(long agentId, String month) {
        return baseMapper.countBonus(agentId,month);
    }

    @Override
    public AgentBonus findByOrderIdAndAgentId(Long orderId, Long agentId) {
        QueryWrapper<AgentBonus> wrapper = new QueryWrapper<AgentBonus>();
        wrapper.eq("order_id",orderId).eq("agent_id",agentId);
        return getOne(wrapper);
    }
}
